package cn.butcher21.view;

import cn.butcher21.dao.SaleDao;
import cn.butcher21.pojo.Sale;
import cn.butcher21.print.Goods;
import cn.butcher21.print.Printer;
import cn.butcher21.print.SalesTicket;
import cn.butcher21.utils.BuildScene;
import cn.butcher21.utils.MakeTradeId;
import cn.butcher21.utils.Pay;
import cn.butcher21.utils.ToPrintList;
import cn.butcher21.window.SuccessWindow;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class WeScanController {

    @FXML
    private TextField payId;

    LoginController login = (LoginController) Context.rootController.get(LoginController.class.getSimpleName());
    SaleController sale = (SaleController) Context.rootController.get(SaleController.class.getSimpleName());


    @FXML
    private void initialize() {
        Thread t_payScan = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                        int i = 0;
                        if (payId.getText().equals("")) {
                            System.out.println((++i) + "正在等待付款码扫描...");
                        } else {
                            System.out.println("已扫描客户" + (++i) + payId.getText());
                            Pay pay = new Pay();
                            System.out.println(sale.sId);
                            pay.weScan(sale.sId, sale.getTotal().getText(), payId.getText(), login.getUsername().getText());
                            payId.setText("已支付请勿重复扫描");
                            List<Goods> format = ToPrintList.format(sale.saleDatas);
                            SalesTicket stk = new SalesTicket(format, login.getUsername().getText(), sale.sId, String.valueOf(format.size()), sale.getTotal().getText(), sale.getTotal().getText(), "0", new Date());
                            new Printer(stk).printer();
                            SaleDao saleDao = new SaleDao();
                            try {
                                saleDao.add(sale.salesToDB);
                                for (Sale sale1 : sale.salesToDB) {
                                    String gid = sale1.getGid();
                                    int count = sale1.getCount();
                                    saleDao.reduce(gid, count);
                                }
                            } catch (SQLException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            } catch (ClassNotFoundException e) {
                                e.printStackTrace();
                            }
                            sale.sId = "";
                            sale.saleDatas.clear();
                            sale.salesToDB.clear();

                            Platform.runLater(new Runnable() {
                                @Override
                                public void run() {
                                    BuildScene.stages.get("weScanStage").close();
                                    new SuccessWindow();
                                }
                            });

                            SaleController o = (SaleController) Context.rootController.get(SaleController.class.getSimpleName());
                            o.loadSaleTableData();

                            break;
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        t_payScan.start();
    }


}

